
var searchFlag = false;
function goSearch(){
	
	// 반복 호출 방지용
	if (searchFlag) {
		return;
	} else {
		searchFlag = true;
	}
	var frm = document.getElementById('sitem_srch');
	var serverId = frm.serverId.value;
	var query = document.getElementById('sitem').value;
	var sort = frm.sort.value;
	var page = frm.page.value;

	if(frm.serverId.value ==  ''){
		alert("서버를 선택해 주세요.");
		return;
	}
	if(query.trim().length > 0){
		frm.query.value = query;
	}else{
		//alert("아이템명을 입력해 주세요.");
		return false;
	}

	frm.submenu.value="";
	getSearchMainList(serverId, query, sort, page);
}


//서버존에서 아이템 검색 시 인기검색어 테이블에 저장하는 프로세스까지 처리
function getSearchMainList(serverId, query, sort, page){

	setFormValue(serverId, query, sort, page, null);
	var frm = document.getElementById('sitem_srch');

	var url="/item/search/searchresult";
	location.href = url + '?serverId=' + serverId + '&query=' + encodeURIComponent(query)+ '&sort=' + encodeURIComponent(sort)+ '&page=' + page;
	
	/*
	new Ajax.Request(url, {
		method: 'post',
		encoding: 'UTF-8',
		asynchronous : false,
		parameters: 'serverId=' + serverId + '&query=' + encodeURIComponent(query)+ '&sort=' + encodeURIComponent(sort)+ '&page=' + page,
		onSuccess: function(transport) {
			try {
				checkResultMsg(serverId, query, transport.responseText);
			} catch(e) {
				alert(e.description);
			}
		},
		onFailure: function(transport) {
			alert("오류가 발생 하였습니다.");
		}
	});
	*/
}

function checkResultMsg(serverId, query, result){

	var cnt = result.itemlist.length;
	if(result.rtnVal == 0){
		var frm = document.getElementById('sitem_srch');
		var targetUrl = "";

		targetUrl = "/item/search/searchresult";
		frm.action = targetUrl;
		frm.submit();

	}else{
		alert("오류가 발생하였습니다. 다시 한번 시도 해주세요.");
	}
}

//아이템 리스트(자음 별)
function getItemListByWord(word, spanId, categoryId){
	var url = '/item/search/getItemTotalListByWord';
	var pars = {};

	pars.word = word;
	pars.categoryId = categoryId;


	new Ajax.Updater(
		spanId,
		url,
		{	method:'get',
			parameters : pars,
			encoding: 'UTF-8',
			evalScripts: true,
			onFailure: function() {
			}
		}
	);
}



function calculateNaviDate(date, daydiff){
	var pageSize = document.getElementById("statisticsbytradefrm").pageSize.value;
	var serverId = document.getElementById("statisticsbytradefrm").serverId.value;
	var categoryType = document.getElementById("statisticsbytradefrm").categoryType.value;
	
	var displaydate = naviCurrDate;
	
	if (date != '')  displaydate = date;
	
	year = displaydate.substring(0,4);
	month = displaydate.substring(4,6) - 1;
	date = displaydate.substring(6,8);
		
	var curdate = new Date(year, month, date);
	curdate.setDate(curdate.getDate() + daydiff); //calculate navi-date

	year = curdate.getFullYear();month = curdate.getMonth() + 1;date = curdate.getDate();
	if(month < 10){	month = '0' + month;}
	if(date < 10){date = '0' + date;}

	naviCurrDate = year+''+month+''+date;
	var naviDateDivStr;

	if(naviCurrDate <= '${naviStartDate}'){
		naviDateDivStr = '<img src="${cl_attribute.ncstatic}/common/blank.gif" alt="" id="yesterday" class="" /> ';
	}else{
		naviDateDivStr = '<a href="javascript:calculateNaviDate('+date+',-1);"><img src="${cl_attribute.ncstatic}/common/blank.gif" alt="" id="tomorrow" class="" /></a> ';
	}
	naviDateDivStr += month + '.' + date;
	if(naviCurrDate >= '${naviEndDate}'){
		naviDateDivStr += ' <img src="${cl_attribute.ncstatic}/common/blank.gif" alt="" id="yesterday" class="on" />';
	}else{
		naviDateDivStr += ' <a href="javascript:calculateNaviDate('+date+',1);"><img src="${cl_attribute.ncstatic}/common/blank.gif" alt="" id="tomorrow" class="on" /></a>';
	}

	document.getElementById("naviDateDiv").innerHTML = naviDateDivStr;

	//updateStatistics(naviDateDivNum, -1);
	
	var pars = {};
	pars.pageSize= pageSize;
	pars.serverId = serverId;
	pars.date = naviCurrDate;
	pars.categoryType = categoryType;
	pars.daydiff = daydiff;

	var url = "/item/rank/getStoreStatisticsRankByTrade";

	new Ajax.Request(url, {
		method: 'post',
		encoding: 'UTF-8',
		asynchronous : false,
		parameters: pars,
		onSuccess: function(transport) {
			try {
				document.getElementById("statisticsbytrade").innerHTML = transport.responseText;	
			} catch(e) {
				alert(e.description);
			}
		},
		onFailure: function(transport) {
			alert("오류가 발생 하였습니다.");
		}
	});

}

function setFormValue(serverId, itemId, itemName, tradeType, enchant){
	var frm = document.getElementById('sitem_srch');
	
		frm.serverId.value = serverId;
		if(itemId.toString().trim().length > 0)			frm.itemId.value = itemId;
		if(itemName.toString().trim().length > 0)		frm.itemName.value = itemName;
		if(tradeType.toString().trim().length > 0)		frm.tradeType.value = tradeType;
		if(enchant != null)
		{
			if(enchant.toString().length >= 0)				frm.enchant.value = enchant;
		}else{
			frm.enchant.value = "";
		}
}

//서버존에서 아이템 검색 시 인기검색어 테이블에 저장하는 프로세스까지 처리
function getStoreMainList(serverId, itemId, itemName, tradeType, enchant){
	setFormValue(serverId, itemId, itemName, tradeType, enchant);
	var frm = document.getElementById('sitem_srch');

	var url="/item/store/getSearchStoreItem";
	new Ajax.Request(url, {
		method: 'post',
		encoding: 'UTF-8',
		asynchronous : false,
		parameters: 'serverId=' + serverId + '&tradeType=' + tradeType + '&itemId=' + itemId + '&itemName=' + encodeURIComponent(itemName),
		onSuccess: function(transport) {
			try {
				checkResultMsg(serverId, itemName, eval(transport.responseText));
			} catch(e) {
				alert(e.description);
			}
		},
		onFailure: function(transport) {
			alert("오류가 발생 하였습니다.");
		}
	});
}

function checkResultMsg(serverId, itemName, result){
	var cnt = result.itemlist.length;
	if(result.rtnVal == 0){
		var frm = document.getElementById('sitem_srch');
		var targetUrl = "";
		if(cnt == 1){
			if(result.itemlist[0].trade == '불가능'){
				document.location.href = "/item/search/none?serverId=" + serverId + "&itemName=" + encodeURIComponent(itemName) + "&result=-3";
			}else{				
				frm.itemId.value = result.itemlist[0].itemId;
				frm.itemName.value = result.itemlist[0].itemName;
				if(result.itemlist[0].pbenchant == '불가능'){
					frm.enchant.value = '';
				} 
				targetUrl = "/item/search/storelist";
				frm.action = targetUrl;
				frm.submit();
			}
		}else if(cnt == 0){
			document.location.href = "/item/search/none?serverId=" + serverId + "&itemName=" + encodeURIComponent(itemName) + "&result=-1";
		}else{
			document.location.href = "/item/search/none?serverId=" + serverId + "&itemName=" + encodeURIComponent(itemName) + "&result=-2";
		}

	}else{
		alert("오류가 발생하였습니다. 다시 한번 시도 해주세요.");
	}
}

function setParameter(){
	var pars = {};
	var frm = document.getElementById('sitem_srch');

	pars.serverId = frm.serverId.value;
	pars.itemId = frm.itemId.value;
	pars.itemName = frm.itemName.value;
	pars.tradeType = frm.tradeType.value;
	pars.enchant = frm.enchant.value;

	return pars;
}

//매매가 상점 리스트 (by item)
function getStoreDetailList(spanId, paramName, paramVal) {
	var pars = setParameter();

	var thisSortColumn = document.getElementById('sortColumn').value;
	var thisSortType = document.getElementById('sortType').value;
	
	pars.sortColumn = thisSortColumn;
	pars.sortType = thisSortType;

	if(paramName == "sortColumn"){
		var sorttype = "desc";
		if(thisSortColumn == paramVal && thisSortType == "desc"){
			sorttype = "asc";
		}

		pars.sortColumn = paramVal;
		pars.sortType = sorttype;
	}

	var pageparam = "";
	if(paramName == "page"){
		pars.page = paramVal;
		pars.pageSize = 20;
	}

	pars.pbenchant = document.getElementById('pbenchant').value;
	pars.enchant = document.getElementById('enchant').value;
	
	var url = '/item/store/getStoreListByItem';

	new Ajax.Updater(
		spanId,
		url,
		{	method:'get',
			parameters : pars,
			encoding: 'UTF-8',
			evalScripts: true,
			onFailure: function() {
			}
		}
	);
}


function getStoreMainListBySubmenu(serverId, itemId, itemName, tradeType, enchant){
	
	document.getElementById('sitem_srch').submenu.value="statistics";
	//if(enchant == ''){
	//	enchant = 0;
	//}
	getStoreIntroMainList(serverId, itemId, itemName, tradeType, enchant);
}

//<A title="제브 레퀴의 마안" href="javascript:getStoreIntroMainList('2', '3779', '제브 레퀴의 마안', 0, '0');">제브 레퀴의 마안</A>
//거래상점 아이템 정보 세팅
function getStoreIntroMainList(serverId, itemId, itemName, tradeType, enchant){

	setFormValue(serverId, itemId, itemName, tradeType, enchant);

	var frm = document.getElementById('sitem_srch');
	
	var url="/item/store/getItemInfoByMain";
	new Ajax.Request(url, {
		method: 'post',
		encoding: 'UTF-8',
		asynchronous : false,
		parameters: 'serverId=' + serverId + '&enchant=' +enchant+ '&itemId=' + itemId.toString().trim() + '&itemName=' + encodeURIComponent(itemName) + '&tradeType=' + tradeType,
		onSuccess: function(transport) {
			try {
				checkResultMsg(serverId, itemName, eval(transport.responseText));
			} catch(e) {
				alert(e.description);
			}
		},
		onFailure: function(transport) {
			alert("오류가 발생 하였습니다.");
		}
	});
}

function getStoreMainListBymain(serverId, itemId, itemName, tradeType, enchant){
	
	document.getElementById('sitem_srch').submenu.value="";
	getStoreIntroMainList(serverId, itemId, itemName, tradeType, enchant);
}

function getStoreMainListBySubmenu(serverId, itemId, itemName, tradeType, enchant){
	document.getElementById('sitem_srch').submenu.value="statistics";
	//if(enchant == ''){
	//	enchant = 0;
	//}
	getStoreIntroMainList(serverId, itemId, itemName, tradeType, enchant);
}

function nav_goto_page_store(page){
	getStoreDetailList('div_storelist', 'page', page);
}

function setStatisticsParameter(){
	var pars = {};
	var frm = document.getElementById('detailSearchForm');

	pars.serverId = frm.serverId.value;
	pars.itemId = frm.itemId.value;
	pars.itemName = frm.itemName.value;
	pars.tradeType = frm.tradeType.value;
	pars.enchant = frm.enchant.value;
	pars.pbenchant = frm.pbenchant.value;
	pars.categoryType = frm.categoryType.value;
	pars.ident = frm.ident.value;
	pars.bless = frm.bless.value;
	pars.elenchant = frm.elenchant.value;

	return pars;
}

//4주 통계 리스트 sorting
function getStoreStatisticsDetailList(spanId, paramName, paramVal) {

	var pars = setStatisticsParameter();

	var thisSortColumn = document.getElementById('sortColumn').value;
	var thisSortType = document.getElementById('sortType').value;

	pars.sortColumn = thisSortColumn;
	pars.sortType = thisSortType;

	if(paramName == "sortColumn"){
		var sorttype = "desc";
		if(thisSortColumn == paramVal && thisSortType == "desc"){
			sorttype = "asc";
		}

		pars.sortColumn = paramVal;
		pars.sortType = sorttype;
	}

	var url = '/item/store/getStoreStatisticsListByItem';

	new Ajax.Updater(
		spanId,
		url,
		{	method:'get',
			parameters : pars,
			encoding: 'UTF-8',
			evalScripts: true,
			onFailure: function() {
			}
		}
	);
}


//매매가 리스트 (by user)
function getStoreListByUser(spanId, userid, storeid, sortColumn, sortType) {
	var pars = setParameter();

	pars.userId = userid;
	pars.tradeId = storeid;
	pars.sortColumn = sortColumn;
		
	if(sortType == 'desc'){
		pars.sortType = 'asc';
	} else {
		pars.sortType = 'desc';
	}
	
	var url = '/item/store/getStoreListByUser';
	new Ajax.Updater(
		spanId,
		url,
		{	method:'get',
			parameters : pars,
			encoding: 'UTF-8',
			evalScripts: true,
			onFailure: function() {
			}
		}
	);
}

function getStoreInfoByUser(spanId, serverId, userid, storeid, sortColumn, sortType, tradeType) {

	var pars = setParameter();
	pars.serverId = serverId;
	pars.userId = userid;
	pars.tradeId = storeid;
	pars.tradeType = tradeType;
	pars.sortColumn = sortColumn;
	
	if(sortType == 'desc'){
		pars.sortType = 'asc';
	} else {
		pars.sortType = 'desc';
	}
	
	var url = '/item/store/getStoreInfoByUser';
	new Ajax.Updater(
		spanId,
		url,
		{	method:'get',
			parameters : pars,
			encoding: 'UTF-8',
			evalScripts: true,
			onFailure: function() {
			}
		}
	);
}

function getElementByIds(tag, id){
	var input = document.getElementsByTagName(tag);
	for(var i=0,result=[];i<input.length;i++){
		if(input[i].id == id) result.push(input[i]);
	}
	return result;
}

function showRandomKeyWord() {
	//var keywordobj = document.getElementsByName("popular_keyword");
	var keywordobj = getElementByIds("span","popular_keyword");
	var pricediv = Math.floor(Math.random()*(keywordobj.length-1));

	for ( var i = 0; i < keywordobj.length; i++ ) {
		if ( pricediv == i ) {
			keywordobj[i].style.display = "block";		
		}	
	}	
}


///////////////////////// 20091207 store update //////////////////////////

function selectStoreServer(serverID, serverName){
		document.getElementById('select_storeserver_name').innerHTML=serverName;
		var frm = document.getElementById('sitem_srch');
		alert(serverID);
		frm.serverId.value = serverID;
}
function selectTradeType(tradetype, tradetypeName){
	var frm = document.getElementById('sitem_srch');
	document.getElementById('select_trade_name').innerHTML=tradetypeName;
	frm.tradeType.value = tradetype;
}
function selectEnchantVal(enchant){
	var frm = document.getElementById('sitem_srch');
	if(enchant.length == 0)	document.getElementById('select_enchant_value').innerHTML="전체";
	else if(enchant == 0)	document.getElementById('select_enchant_value').innerHTML=enchant;
	else if(enchant > 0)	document.getElementById('select_enchant_value').innerHTML="+" + enchant;
	else	document.getElementById('select_enchant_value').innerHTML=enchant;
	frm.enchant.value = enchant;
}

function getMyStore(serverId, charName){
	getMyStoreList('mystore0', serverId, charName, 0);
	getMyStoreList('mystore1', serverId, charName, 1);
	getMyStoreHistoryList('mystore_historylist', serverId, charName, 'tradeType' , 0, 'serverzone');
}
//내 상점  리스트
function getMyStoreList(spanId, serverId, charName, tradeType){
	var pars = {};
	pars.serverId = serverId;
	pars.userName = charName;
	pars.tradeType = tradeType;
	var url = '/item/mystore/getMyStoreList';
	new Ajax.Updater(
		spanId,
		url,
		{	method:'get',
			parameters : pars,
			encoding: 'UTF-8',
			evalScripts: true,
			onFailure: function() {
			}
		}
	);
}

//내 상점 히스토리 리스트
function getMyStoreHistoryList(spanId, serverId, charName, paramName, paramVal, pageType){
	var pars = {};
	pars.serverId = serverId;
	pars.userName = charName;
	pars.tradeType = document.getElementById('mystoreTradeType').value;
	if(paramName == "page"){
		pars.page = paramVal;
	}
	if(paramName == "tradeType"){
		pars.tradeType = paramVal;
	}

	pars.pageType = pageType;


	var url = '/item/mystore/getMyStoreHistoryList';
	new Ajax.Updater(
		spanId,
		url,
		{	method:'get',
			parameters : pars,
			encoding: 'UTF-8',
			evalScripts: true,
			onFailure: function() {
			}
		}
	);
}

function getGaiaStoreIntroMainList (itemId, itemName, tradeType, enchant) {
	var serverId = document.getElementById('serverId').value;
	getStoreIntroMainList(serverId, itemId, itemName, tradeType, enchant)
}

//날짜별 상점 히스토리 리스트
function getStoreHistoryListByDate(spanId, paramName, paramVal){
	var pars = setStatisticsParameter();

	if(paramName == "date"){
		pars.date = paramVal;
		pars.sortColumn = "time";
		pars.sortType = "desc";
		pars.page = 1;
		pars.pageSize = 30;
	}else{
		var frm = document.getElementById('detailSearchForm');
		pars.date = frm.date.value;

		thisSortColumn = frm.sortColumn.value;
		thisSortType = frm.sortType.value;

		pars.sortColumn = thisSortColumn;
		pars.sortType = thisSortType;

		if(paramName == 'page'){
			pars.page= paramVal;
			pars.pageSize = frm.pageSize.value;

		}else{
			if(paramVal != ''){
				var sorttype = "desc";
				if(thisSortType == "desc")	sorttype = "asc";

				pars.sortColumn = paramVal;
				pars.sortType = sorttype;
			}
		}
	}

	var url = '/item/store/getStoreHistoryListByDate';
	new Ajax.Updater(
		spanId,
		url,
		{	method:'get',
			parameters : pars,
			encoding: 'UTF-8',
			evalScripts: true,
			onFailure: function() {
			}
		}
	);
}

function nav_goto_page_storehistorybydate(page){
	getStoreHistoryListByDate('div_storelist', 'page', page);
}

//4주 통계 리스트
function detailStatisticsSearch(){

	frm = document.detailSearchForm;

	ident_tmp = '';
	for(i=0; i<frm.elements.length; i++){
		if(frm.elements[i].name == 'ident_tmp' && frm.elements[i].checked){
			ident_tmp = frm.elements[i].value;
		}
	}
	elenchant = '';
	for(i=0; i<frm.elements.length; i++){
		if(frm.elements[i].name == 'elenchant_tmp' && frm.elements[i].checked){
			elenchant = frm.elements[i].value;
		}
	}

	if(ident_tmp == ''){
		alert('상태를 선택해 주세요.');
		return;
	}

	frm.elenchant.value = elenchant;
	if(ident_tmp > 0){
		frm.ident.value = 1;
		if(ident_tmp == 1){
			frm.bless.value = 1; //확인
		}else{
			frm.bless.value = 0; //확인(축)
		}
	}else{
		frm.ident.value = 0;
		frm.bless.value ='';
	}

	frm.action = "/item/search/storelist";
	frm.submit();
}

//4주 통계 summery 에서 최저가 최고가 링크
function goStatisticsListBySorting(sortColumn, sortType, ident, bless, elenchant){
	var pars = setParameter();

	parameters = 'serverId='+pars.serverId + '&itemId='+pars.itemId+'&itemName='+encodeURIComponent(pars.itemName)+'&tradeType='+pars.tradeType+'&enchant='+pars.enchant+'&submenu=statistics&sortColumn='+sortColumn+'&sortType='+sortType;
	parameters = parameters + '&ident='+ident +'&bless='+bless+'&elenchant='+elenchant;
	location.href = '/item/search/storelist?'+parameters;
}

function getSortStoreIntroMainList(sortColumn, sortType, serverId, itemId, itemName, tradeType, enchant){
	var parameters= "";
	parameters = 'serverId='+serverId + '&itemId='+itemId+'&itemName='+encodeURIComponent(itemName)+'&tradeType='+tradeType+'&enchant='+enchant+'&submenu=&sortColumn='+sortColumn+'&sortType='+sortType;
	location.href = '/item/search/storelist?'+parameters;
}


//mystore navi 내상점현황
function nav_goto_page_mystore(page){
	var serverId = document.getElementById('myStoreServerId').value;
	var charName = document.getElementById('userName').value;
	var pagetype = document.getElementById('pagetype').value;
	getMyStoreHistoryList('mystore_historylist', serverId, charName, 'page', page, pagetype);
}

